from lxml import etree
import urllib.request as req


def create_request(page):
    headers = {
        'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/107.0.0.0 Safari/537.36 Edg/107.0.1418.35',
        'cookie': 'Hm_lvt_7d4ad7d5cab65121c1deb1c3da53d9e4=1668399657; Hm_lpvt_7d4ad7d5cab65121c1deb1c3da53d9e4=1668399702'
        
    }
    url = 'https://www.boxmp4.com/list/5-'+str(page)+'.html'

    request = req.Request(url=url, headers=headers)
    return request


def get_content(request):

    res = req.urlopen(request)
    con = res.read().decode('utf-8')
    return con


def down_load(con):
    tree = etree.HTML(con)
    answer = tree.xpath("//div[@class='text_info']/h2/a/text()")
    with open(file="boxmp4com.txt",mode="a",encoding="utf-8") as fp:
        for i in range(1,len(answer)):
            fp.write(answer[i])
            fp.write("\n")


if __name__ == "__main__":
    start_page = int(input('请输入起始页码'))
    end_page = int(input('请输入结束页码'))
    for page in range(start_page, end_page+1):
        print(page)
        request = create_request(page)
        con = get_content(request)
        down_load(con)
